#include "mode_cooling.h"
#include "actuator_control.h"
#include "sensors.h"
#include <stdio.h>

// 制冷模式状态
static struct {
    uint32_t start_time;              // 制冷模式开始的时间（秒级）
    bool pump_initialized;            // 膨化泵是否已经初始化过
    bool pump_state;                  // 膨化泵当前状态（开/关）
    uint32_t pump_timer;              // 膨化泵的定时器（秒级），用于每隔一段时间切换泵的开关
    bool compressor_started;          // 压缩机是否已经启动
    uint32_t compressor_delay_end;    // 压缩机允许启动的时间点（毫秒），避免立即启动，要延迟一段时间
    const SystemConfig *config;       // 指向系统配置的指针，存放用户或系统设定参数
} cooling_state;

// 初始化制冷模式
void mode_cooling_init(const SystemConfig *config) {
    if (config) {
        cooling_state.config = config;
    }
    
    // 初始化状态变量
    cooling_state.start_time = 0;
    cooling_state.pump_initialized = false;
    cooling_state.pump_state = false;
    cooling_state.pump_timer = 0;
    cooling_state.compressor_started = false;
    cooling_state.compressor_delay_end = 0;
}

// 进入制冷模式
void mode_cooling_enter(void) {
    printf("Entering cooling mode\n");
    
    // 记录开始时间
    cooling_state.start_time = get_system_time_sec();
    cooling_state.pump_initialized = false;
    cooling_state.compressor_started = false;
    cooling_state.compressor_delay_end = get_system_time_ms() +  cooling_state.config->compressor_on_delay*1000;  // 8秒延迟
    
    // 立即开启膨化泵、主电机和预冷缸制冷阀
    actuator_set_pump(true); //开启膨化泵
    actuator_set_main_motor(true);  //开启主电机
    actuator_set_cooling_valve(true);   //开启制冷阀
}

// 退出制冷模式
void mode_cooling_exit(void) {
    printf("Exiting cooling mode\n");
    
    // 关闭所有相关设备
    actuator_set_pump(false);
    actuator_set_main_motor(false);
    actuator_set_cooling_valve(false);
    actuator_set_compressor(false);
}

// 处理制冷模式逻辑
ModeStatus mode_cooling_process(void) {
    uint32_t current_time_ms = get_system_time_ms();
    uint32_t current_time_sec = get_system_time_sec();
    int current = sensor_get_current();
    
    // 检查是否超过最大制冷时间
    if (current_time_sec - cooling_state.start_time > cooling_state.config->cooling_time_limit) {
        printf("Cooling overheat protection activated (over %dmin)\n", 
               cooling_state.config->cooling_time_limit / 60);
          cooling_state.compressor_started = false;
         actuator_set_compressor(false);
         actuator_set_pump(false);
        return MODE_STATUS_ERROR;  // 返回错误状态表示过热保护
    }
    
    // 检查电流是否达到设定硬度(停止条件)
    if (cooling_state.compressor_started && current >= cooling_state.config->current_threshold) {

        printf("Current reached threshold (%dmA), stopping cooling\n", current);
         cooling_state.compressor_started = false;
         actuator_set_compressor(false);
         actuator_set_pump(false);
    }
    
    // 检查电流是否低于阈值的0.9倍(开启条件)
    if (current < cooling_state.config->current_threshold * cooling_state.config->current_hysteresis) {
        // 处理膨化泵控制逻辑
        if (!cooling_state.pump_initialized) {
            // 首次开启，启动计时
            cooling_state.pump_initialized = true;
            cooling_state.pump_timer = current_time_sec;
             actuator_set_pump(true);
             cooling_state.pump_state = true;
        } else {
            // 检查是否需要切换膨化泵状态(50秒左右切换)
            if (current_time_sec - cooling_state.pump_timer >= 50) 
            {
                cooling_state.pump_state = !cooling_state.pump_state;
                actuator_set_pump(cooling_state.pump_state);
                printf("Pump %s (50s interval)\n", cooling_state.pump_state ? "ON" : "OFF");
                cooling_state.pump_timer = current_time_sec;  // 重置计时器
            }
        }
        
        // 处理压缩机延迟启动
        if (!cooling_state.compressor_started && current_time_ms >= cooling_state.compressor_delay_end) {
            actuator_set_compressor(true);
            cooling_state.compressor_started = true;
            printf("Compressor started after 8s delay\n");
        }
    }
    
    return MODE_STATUS_RUNNING;  // 继续运行
}
    